package cn.iocoder.yudao.module.resource;

import com.alibaba.dashscope.audio.ttsv2.SpeechSynthesisParam;
import com.alibaba.dashscope.audio.ttsv2.SpeechSynthesizer;
import com.alibaba.dashscope.audio.ttsv2.enrollment.Voice;
import com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentService;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

// https://bailian.console.aliyun.com/?tab=model#/api-key
// https://help.aliyun.com/zh/model-studio/cosyvoice-java-api?spm=a2c4g.11186623.help-menu-2400256.d_2_4_0_0.24db3560zNdt3f
public class AliVoiceCloneClient {

    public static String apiKey = "sk-e97333b592b84ac18b97688759094454";  // 如果您没有配置环境变量，请在此处用您的API-KEY进行替换
    private static String fileUrl = "https://lmrobot-pri.cn-wlcb.ufileos.com/role/audio/10号我不甜105.mp3?UCloudPublicKey=STS.3mEUdSrDwkSajR7Xzr9GMj&Expires=1745540422&Signature=RVXphJGzveqFNezck0DIzjzTApY%3D&SecurityToken=AAH149nua32P-1yJqqAT50S5Tv0TY3cWjeotoxkfB0q-VHaLmA7HDrL-RDqV7ZtUd6vaxF52IRcwEJMpDP_ms8U1bBOdJjvmMRhYJf2d3TRRngspPs1BizKsHPv_vkufPWeY9ByqWePJlSHiLkg7YqNqyaOFiaVgAO5DxP8QueibVqITEM3WhJDIj3I-kBtNqTIFyUVLY4-JaNsSmm8gCYPryQc";  // 请按实际情况进行替换
    private static String prefix = "lm";
    private static String targetModel = "cosyvoice-v2";

    public static void main(String[] args)
            throws NoApiKeyException, InputRequiredException {

        AliVoiceCloneClient audioCopy = new AliVoiceCloneClient();
        // 复刻声音
        String voiceId = audioCopy.createVoice(fileUrl);
        // your voice id is cosyvoice-lm-ebaa1a7212b34343b4b92c32778af43b
        // 使用复刻的声音来合成文本为语音
        String text = "你在干嘛";

        String path = "/Users/edy/Downloads/ai/" + text + ".mp3";
//        String path = "E:\\ai\\output\\" + text + ".mp3";
        audioCopy.copyVoice(voiceId, text, path);
        System.exit(0);
    }

    public void copyVoice(String voiceId, String text, String path)
            throws NoApiKeyException, InputRequiredException {
        // 复刻声音
        // 使用复刻的声音来合成文本为语音
        SpeechSynthesisParam param = SpeechSynthesisParam.builder()
                .apiKey(apiKey)
                .model(targetModel)
                .voice(voiceId)
                .build();
        SpeechSynthesizer synthesizer = new SpeechSynthesizer(param, null);
        ByteBuffer audio = synthesizer.call(text);
        // 保存合成的语音到文件
        System.out.println("TTS RequestId: " + synthesizer.getLastRequestId());
        File file = new File(path);
        try (FileOutputStream fos = new FileOutputStream(file)) {
            fos.write(audio.array());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String createVoice(String fileUrl) throws NoApiKeyException, InputRequiredException {
        VoiceEnrollmentService service = new VoiceEnrollmentService(apiKey);
        Voice myVoice = service.createVoice(targetModel, prefix, fileUrl);
        System.out.println("RequestId: " + service.getLastRequestId());
        // your voice id is cosyvoice-lm-ebaa1a7212b34343b4b92c32778af43b
        System.out.println("your voice id is " + myVoice.getVoiceId());
        return myVoice.getVoiceId();

        //return "cosyvoice-v2-lm-7e5c18777bd94f41a7f0894df7f1cddb";
    }

}
